<?php

namespace App\Api\Logistics;

/**
 * https://docs.qq.com/doc/DRUlEamNJWFFhVmVR
 */

use App\Api\SetWayAse;
use App\Console\Commands\Commission;
use Illuminate\Support\Facades\Http;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class K100 extends KChannel
{
    public $pname;
    public $log;
    public $root;
    private $url;
    private $key;
    private $customer;

    public function __construct()
    {
        $this->pname = 'K100';
        $this->log = new Logger('K100');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/K100.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->root = new Logger('K100RootLog');
        $this->root->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/K100ProRootLog.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->url = config('express.apiPollUrl');
        $this->key = config('express.key');
        $this->customer = config('express.customer');
    }

    public function supscr($data)
    {
        // var_dump($data);
        // die();
        if (empty($data['express_company']) || empty($data['express_number'])) {
            return $this->resMsg(00, '物流公司或物流快递不能为空');
        }
        if ($data['express_company'] == '无需物流' && $data['express_company'] == '营业厅上门开卡') {
            return $this->resMsg(101, '不需要物流');
        }
        $code = '';
        // $code = 'shentong';
        if (!empty($data['express_company']) && $data['express_company'] == 'EMS') {
            $code = 'ems';
        } else if (!empty($data['express_company']) && strstr($data['express_company'], '顺丰')) {
            $code = 'shunfeng';
        } elseif (!empty($data['express_company']) && strstr($data['express_company'], 'EMS')) {
            $code = 'ems';
        } elseif (!empty($data['express_company']) && strstr($data['express_company'], '邮政')) {
            $code = 'ems';
        } else if (!empty($data['express_company']) && $data['express_company'] == 'ems') {
            $code = 'ems';
        } else if (!empty($data['express_company']) && $data['express_company'] == '京东') {
            $code = 'jd';
        } elseif (!empty($data['express_company']) && strstr($data['express_company'], '京东')) {
            $code = 'jd';
        }
        if (empty($code)) {
            return $this->resMsg(103, '该快递不在查询列表中');
        }
        // if (empty($this->grant_type)) {
        //   return $this->resMsg(104, '第三方访问类型不能为空');
        // }
        // $time = date('YmdHis');
//        $return_url = DB::table('system')->where(['name' => 'express_url'])->first();
        $url = config('express.pollBackUrl');
        $form = [
            'company' => $code,
            'number' => $data['express_number'],
            'key' => $this->key,
//            'customer' => $this->customer,
            'parameters' => [
                'callbackurl' => $url,
                'resultv2' => 1
            ],
        ];
        $post_data = array();
        $post_data['schema'] = 'json';
        $post_data['param'] = json_encode($form, JSON_UNESCAPED_UNICODE);
        $result = $this->requestSms($this->url, $post_data);
        if (!empty($result['returnCode']) && $result['returnCode'] == 200) {
            return ['code' => 200, 'msg' => '发送成功', 'data' => $result];
        }
        $this->root->info('快递请求失败参数:' . json_encode($post_data) . '返回结果:' . json_encode($result));
        return ['code' => -1, 'msg' => '发送失败'];
    }
    // 查看回复信息
    //发送请求
    public function requestSms($url, $data)
    {
        $response = Http::withOptions([
//            'debug' => true,
            'timeout' => 30,
            'connect_timeout' => 30,
            'read_timeout' => 30
        ])->asForm()->timeout(30)->post($url, $data);
        return $response->json();

//        $curl = curl_init();
//        //设置抓取的url
//        curl_setopt($curl, CURLOPT_URL, $url);
//        //设置头文件的信息作为数据流输出
//        curl_setopt($curl, CURLOPT_HEADER, 0);
//        //设为TRUE把curl_exec()结果转化为字串，而不是直接输出
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
//        //传递 header 头
//        // curl_setopt($curl, CURLOPT_HTTPHEADER, []);
//        //对认证证书来源的检查
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//        //从证书中检查SSL加密算法是否存在
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//        //post请求
//        curl_setopt($curl, CURLOPT_POST, 1);
//        //设置提交的字符串
//        // if ($isAccress) {
//        //   curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($form)); //http_build_query(
//        // } else {
//        //   curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($form)); //http_build_query(
//        // }
//        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); //http_build_query(
//
//        //设置超时时间
//        curl_setopt($curl, CURLOPT_TIMEOUT, 60000);
//        $output = curl_exec($curl);
//        // var_dump($output,1111111);
//        if (curl_errno($curl)) {
//            return ['status' => -1, 'msg' => curl_error($curl)];
//            return false;
//        }
//        curl_close($curl);
//        $this->root->info('发送返回参数' . $output);
//        // $res = xml_to_data($output);
//        // return strToKeyValArr($output);
//        return json_decode($output, true);
    }
}
